<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

class OwnserviceModelTodaytransactions  extends JModel
{
	/**
	 * Today Transactions data array
	 *
	 * @var array
	 */
	var $_data = null;

	/**
	 * Today Transactions total
	 *
	 * @var integer
	 */
	var $_total = null;

	/**
	 * Pagination object
	 *
	 * @var object
	 */
	var $_pagination = null;
	
	/**
	 * Constructor
	 *
	 * @since 1.5
	 */

	function __construct()
	{
		parent::__construct();
		
		global $mainframe, $option;
		
		//get the number of entries from session
		$limit			= $mainframe->getUserStateFromRequest($option.'.todaytransaction.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');		
		$limitstart		= $mainframe->getUserStateFromRequest($option.'.todaytransaction.limitstart', 'limitstart', 0, 'int' );
		
		$limitstart 	= ( $limit != 0 ? (floor($limitstart / $limit) * $limit) : 0 );
		
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);
	}

	/**
	 * Method to get Data
	 *
	 * @access public
	 * @return mixed
	 */
	function getData()
	{
			// Lets load the transaction if it doesn't already exist
		if (empty($this->_data))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
			if ((int)$this->getState('limitstart') < (int)$this->_total) {
				$this->_data = $this->_getList( $query, $this->getState('limitstart'), $this->getState('limit') );
			} else {
				$this->_data = $this->_getList( $query, 0, $this->getState('limit') );
			}
		}
		echo $query;
		return $this->_data;
	}
	
	
	/**
	 * Total nr of Today transaction
	 *
	 * @access public
	 * @return integer
	 */
	function getTotal()
	{
		// Lets load the total nr if it doesn't already exist
		if (empty($this->_total))
		{
			$query = $this->_buildQuery();
			$this->_total = $this->_getListCount($query);
		}

		return $this->_total;
	}
	
	
	/**
	 * Builds the query
	 *
	 * @access public
	 * @return string
	 */
	function _buildQuery() {
		// Get the WHERE and ORDER BY clauses for the query
		$where			= $this->_buildItemWhere();
		$orderby		= $this->_buildItemOrderBy();
		
		$query = ' SELECT '
				.' t.*, '
				.' u.username, p.id AS memberid, a.bank_name, a.branch_city, '
				.' r.iscore ' 
				.' FROM #__ownservice_transactions AS t '
				.' LEFT JOIN #__users AS u ON u.id=t.user_id '
				.' LEFT JOIN #__ownservice_users AS p ON p.user_id=u.id '
				.' LEFT JOIN #__ownservice_accounts AS a ON a.account_no = t.account '
				.' LEFT JOIN #__ownservice_exchangerates AS r ON r.code=t.rate_code '
				. $where
				.' GROUP BY t.id '
				. $orderby
				;
				
				
		return $query;
	}
	
	/**
	 * Build the order clause
	 *
	 * @access private
	 * @return string
	 */
	function _buildItemOrderBy()
	{
		global $mainframe, $option;
		
		$filter_order		= $mainframe->getUserStateFromRequest($option.'.todaytransaction.filter_order', 'filter_order', 't.invoice', 'cmd');;
		$filter_order_dir	= $mainframe->getUserStateFromRequest($option.'.todaytransaction.filter_order_Dir', 'filter_order_Dir', 'DESC', 'cmd');
		
		$orderby 	= ' ORDER BY '.$filter_order.' '.$filter_order_dir.', t.created';

		return $orderby;
	}
	
	/**
	 * Method to build the WHERE clause
	 *
	 * @access private
	 * @return array
	 */
	function _buildItemWhere( )
	{
		global $mainframe, $option;
		
		//Initialise variables
		$user	= & JFactory::getUser();
		$owuser = & Ownservice::getUser();
		$db 	= & JFactory::getDbo();
		
		//get current date
		$now		= date('Y-m-d');
		$tomorrow 	= date('Y-m-d', mktime(0,0,0,date('m'),date('d')+1,date('Y')));
		$where = array();
		
		$where[] = 't.state=1';
		
		$where[] = '(t.requested_date >= '.$this->_db->Quote($now.' 00:00:00').' AND t.requested_date <= '.$this->_db->Quote($tomorrow.' 00:00:00').')';
		
		//check access 
		if($user->gid < 25) {
			if( $owuser->get('branch_id', 0) && $user->get('id', 0))
			{	
				$where[] = 't.branch_id ='.$owuser->get('branch_id');
			}
		}
		
		$where 		= ( count( $where ) ? ' WHERE ' . implode( ' AND ', $where ) : '' );
		
		return $where;
		
	}
}